package xj.model.uml;

import static xj.model.uml.UMLModelConstants.ATTR_STATIC;
import static xj.model.uml.UMLModelConstants.ATTR_TYPE;

public abstract class Feature 
  extends NamedElement {

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
    setAttribute(ATTR_TYPE, type);
  }

  public boolean isStatic() {
    return static_;
  }

  public void setStatic(boolean s) {
    static_ = s;
    setAttribute(ATTR_STATIC, s);
  }

  @Override
  public String toString() {
    return "Feature name=" + name + " type=" + type;
  }

  @Override
  public Object clone() throws CloneNotSupportedException {
    Feature f = (Feature) super.clone();
    f.setVisibility(getVisibility());
    f.setType(type);
    f.setStatic(static_);
    return f;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null ||
	this.getClass() != o.getClass()) { 
      return false; 
    }

    if (o instanceof Feature) {
      Feature e = (Feature) o;
      if (name == null && e.name != null || 
	  name != null && !name.equals(e.name)) {
	return false;
      }
      if (type == null && e.type != null || 
	  type != null && !type.equals(e.type)) {
	return false;
      }
      return getVisibility() == e.getVisibility();
    }
    return false;
  }

  @Override
  public int hashCode() {
    int c = 0;
    if (name != null) {
      c += (name.hashCode() & 0xFFFF0000);
    }
    if (type != null) {
      c += (type.hashCode() & 0xFFFF);
    }
    if (getVisibility() != null) {
      c += getVisibility().hashCode();
    }
    return c;
  }

  public String toString(boolean withStereotype) {
    String base = toString();
    if (withStereotype) {
      String st = getStereotypeString();
      if (st != null) {
	return "\u00AB" + st + "\u00BB " + base;
      }
    }
    return base;
  }

  protected Feature() {
  }

  protected Feature(String name) {
    super(name);
  }

  protected Feature(String name, String type) {
    super(name);
    setType(type);
  }

  protected Feature(String name, String type, Visibility visibility) {
    super(name, visibility);
    setType(type);
  }

  protected String type;

  protected boolean static_;

}
